package com.moli.iov.down.enums;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  命令枚举 对外用
 * </p>
 *
 * @author: zql
 * @Date: 2018/12/4 10:50
 */
public enum OperateTypeEnum {

    /**
     * 开门
     */
    OPEN_DOOR(1, "开门", true),

    /**
     * 关门
     */
    CLOSE_DOOR(2, "关门", true),

    /**
     * 寻车
     */
    SEARCH_CAR(3, "寻车", true),

    /**
     * 设为租赁
     */
    SET_LEASE(4, "设为租赁", true),

    /**
     * 设为非租赁
     */
    SET_UN_LEASE(5, "设为非租赁", true),

    /**
     * 钥匙使能
     */
    KEY_ENABLE(6, "钥匙使能", true),

    /**
     * 钥匙禁用
     */
    KEY_DISABLED(7, "钥匙禁用", true),

    /**
     * 开启动力
     */
    OPEN_POWER(8, "开启动力", true),

    /**
     * 关闭动力
     */
    POWER_OFF(9, "关闭动力", true),

    /**
     * 域名/IP
     */
    CHANGE_IP(10, "平台域名或IP", true),

    /**
     * 平台端口号
     */
    CHANGE_PORT(11, "平台端口", true),

    /**
     * 地址和端口
     */
    CHANGE_IP_PORT(12, "平台域名或IP和平台端口", true),

    /**
     * 上报频率
     */
    FREQUENCY(13, "实时数据上报频率", true),

    /**
     * 重启终端
     */
    RESTART(14, "重启终端", true),

    /**
     * 恢复出厂设置
     */
    RESUME_FACTORY(15, "终端恢复出厂设置", true),

    /**
     * T-Box升级
     */
    UPGRADE(16, "终端升级", true),

    /**
     * 开窗
     */
    OPEN_WINDOW(17, "开窗", true),

    /**
     * 关窗
     */
    CLOSE_WINDOW(18, "关窗", true),

    /**
     * 停止
     */
    STOP_WINDOW(19, "停止", true);

    private int flag;

    private String msg;

    private boolean isRetry;

    OperateTypeEnum(int flag, String msg, boolean isRetry){
        this.flag = flag;
        this.msg = msg;
        this.isRetry = isRetry;
    }

    /**
     * Get vehicle control all command enums.
     * @return Vehicle control all command enums collection.
     */
    public static List<OperateTypeEnum> getCtlVehicleEnums(){
        return Arrays.stream(OperateTypeEnum.values()).filter(
                operateTypeEnum -> operateTypeEnum.getFlag() < 3 ||
                        ( operateTypeEnum.getFlag() > 3 && operateTypeEnum.getFlag() < 10) ||
                        ( operateTypeEnum.getFlag() > 16 && operateTypeEnum.getFlag() < 19))
                .collect(Collectors.toList());
    }

    /**
     * 获取命令名称
     * @param flag
     * @return
     */
    public static String getMsg(int flag){
        for( OperateTypeEnum e : OperateTypeEnum.values()){
            if(e.flag == flag){
                return e.msg;
            }
        }
        return null;
    }

    /**
     * 判断命令是否需要补发
     * @param operatorType
     * @return
     */
    public static Boolean isRetry(int operatorType){
         /**
          * 开门、关门、寻车、设为租赁/非租赁、动力开启/动力关闭、钥匙使能/钥匙禁用、
          * 实时上报频率、重启终端、恢复出厂设置、终端升级、修改IP端口
          * 需要补发
          */
        for(OperateTypeEnum e : OperateTypeEnum.values()){
            if(e.flag == operatorType){
                return e.isRetry;
            }
        }
        return false;
    }

    public static List<OperateTypeEnum> getControlCommandList(){

        List<OperateTypeEnum> controls = new ArrayList<>();
        for(OperateTypeEnum e : OperateTypeEnum.values()){
            if(e == CHANGE_IP ||
                    e == CHANGE_PORT ||
                    e == CHANGE_IP_PORT ||
                    e == RESTART ||
                    e == RESUME_FACTORY ||
                    e == UPGRADE ||
                    e == FREQUENCY ){
                continue;
            }
            controls.add(e);
        }
        return controls;
    }
    /**
     * 控制命令集合
     * @return
     */
    public static List<Integer> getControlCommands(){

        List<Integer> controls = new ArrayList<>();
        for(OperateTypeEnum e : OperateTypeEnum.values()){
           if(e == CHANGE_IP ||
                e == CHANGE_PORT ||
                e == CHANGE_IP_PORT ||
                e == FREQUENCY ||
                e == RESTART ||
                e == RESUME_FACTORY ||
                e == UPGRADE){
               continue;
           }
            controls.add(e.getFlag());
        }
        return controls;
    }

    /**
     * 设置命令命令 （不包含升级）
     * @return
     */
    public static List<Integer> getSetCommands(){
        return Arrays.asList(CHANGE_IP_PORT.getFlag(),
                            FREQUENCY.getFlag(),
                            RESTART.getFlag(),
                            RESUME_FACTORY.getFlag(),
                            UPGRADE.getFlag());
    }

    public int getFlag() {
        return flag;
    }

    public String getMsg() {
        return msg;
    }

    public boolean isRetry() {
        return isRetry;
    }
}
